﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.SellStocks;
using MangoMes.Commont.Domain.Suppliers;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.SellStockSearchWrite
{
    public class SellStockCreateHandler : IRequestHandler<SellStockCreateCommand, ResultsDTO>
    {
        private readonly IBateRepository<SellStock> _sellstockRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sellstockRepository"></param>
        public SellStockCreateHandler(IBateRepository<SellStock> sellstockRepository)
        {
            _sellstockRepository = sellstockRepository;
        }
        /// <summary>
        /// 销售出库新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<ResultsDTO> Handle(SellStockCreateCommand request, CancellationToken cancellationToken)
        {
            DapperHelper dapperHelper = new DapperHelper();
            ResultsDTO res = new ResultsDTO { Code = 0 };
            SellStock per = new SellStock();
            per.SellStockId = request.SellStockId;
            per.SellStockNo = request.SellStockNo;
            per.SellStockName = request.SellStockName;
            per.SellStockIndent = request.SellStockIndent;
            per.ClientId = request.ClientId;
            per.SellStockTime = request.SellStockTime;
            per.SellStockStart = request.SellStockStart;
            per.SellStockRemark = request.SellStockRemark;
            per.SellStockState = 0;
            per.ProductsSaleId = 0;
            string conn = dapperHelper.MangoMesWrite;
            string str = "insert into sellstock values(@SellStockId,@SellStockNo,@SellStockName,@SellStockIndent,@ClientId,@SellStockTime,@SellStockStart,@SellStockRemark,@SellStockState,@ProductsSaleId)";
            res.Code = await _sellstockRepository.Add(str, per, conn);
            return res;
        }
    }
}
